\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[cm]{fullpage}
\usepackage{amsmath}
\usepackage{amssymb}

%opening
\title{Assembly 2 Docs }
\author{Hamish}

\begin{document}

\maketitle

\begin{abstract}

Documents for personal use to help track of the maths behind the Assembly 2 workbench add-on for FreeCAD~v0.15+.

\end{abstract}

\tableofcontents

% \vfil

% \pagebreak


\section{Solver approach}

Adjust the placement variables (position and rotation variables, 6 for each object) as to satisfy all constraints.
Approach entails reducing the systems degrees-of-freedom, one constraint at a time until all constraints are processed.
Ideally, this degrees-of-freedom can be identified, so that they can be adjusted with having to check/reprocess previous constraints.
For non-simple systems this is not practical however as there are to many combinations to hard-code.

Therefore a hierarchical constraint system is used.
When attempting the solve the current constraint, the placement variables are also adjusted/refreshed according to the previous constraints to allow for non-perfect degrees-of-freedom.
This is done in a hierarchical way, with parent constraints minimally adjusting the placement variables as to satisfy the assembly constraints.

\section{Plane mating constraint}

2 parts 
\begin{enumerate}
 \item rotating objects as to align selected faces (not done if face and vertex are selected), \textbf{axis alignment union}
 \item moving the objects as to specified offset is satisfied, \textbf{plane offset union}
\end{enumerate}


\section{plane offset union - analytical solution}

inputs
\begin{description}
 \item[~~~~$\mathbf{a}$] - normal vector of reference face.
 \item[~~~~$\mathbf{p}$] - point on object 1.
 \item[~~~~$\mathbf{q}$] - point on object 2. 
 \item[~~~~$\alpha$] - specified offset
 \item[~~~~$\mathbf{d}_1,\mathbf{d}_2...$] - linear motion degree-of-freedom for object 1/2 (max of 3, min of 1)
\end{description}
required displacement in the direction of  $\mathbf{a}$:
\begin{equation}
 r = \mathbf{a} \cdot (\mathbf{p} - \mathbf{q}) - \alpha
\end{equation}
require components for each $\mathbf{d}$, $v_1,v_2,...$ therefore equal to
\begin{equation}
 \mathbf{a} \cdot ( v_1 \mathbf{d}_1 +  v_2 \mathbf{d}_2 +  v_3 \mathbf{d}_3 )  = r
\end{equation}
which has infinite solutions if more than one degree-of-freedom with $\mathbf{d} \cdot \mathbf{a} \ne 0 $

Therefore looking for least norm solution of:
\begin{equation}
 r = a_x ( v_1 d_{1,x} + v_2 d_{2,x } + \dots) + a_y ( v_1 d_{1,y} + v_2 d_{2,y } + \dots) + a_z ( v_1 d_{1,z} + v_2 d_{2,z } + \dots)
\end{equation}
which gives
\begin{align}
 \begin{bmatrix}
  (a_x d_{1,x} + a_y d_{1,y} + a_z d_{1,z}) &
  (a_x d_{2,x} + a_y d_{2,y} + a_z d_{2,z}) &
  (a_x d_{3,x} + a_y d_{3,y} + a_z d_{3,z})  
 \end{bmatrix}
 \begin{bmatrix}
  v_1  \\ v_2 \\ v_3 
 \end{bmatrix} 
 = [ r ] \\
 \begin{bmatrix}
  \mathbf{a} \cdot \mathbf{d}_1 & \mathbf{a} \cdot \mathbf{d}_2 & \mathbf{a} \cdot \mathbf{d}_3 
 \end{bmatrix}
 \begin{bmatrix}
  v_1  \\ v_2 \\ v_3 
  \end{bmatrix}  = r \\ 
 A \mathbf{v} = [r]
\end{align}
then solve for least norm using numpy.linalg.lstsq



\section{Numerically Reducing the System's Degrees-of-Freedom}

New degrees-of-freedom need to be determined which allow for adjustment without the constraint's equality function, $h$, being violated.
Therefore $h$ as a function of the current DOF $\mathbf{x}$ needs be satisfied so that
\begin{equation}
 h(\mathbf{x}) = 0
\end{equation}

The DOF of the new system will therefore result in a change of $\mathbf{x}$ ( $\Delta \mathbf{x}$ ) so that
\begin{equation}
 h(\mathbf{x} + \Delta \mathbf{x}) = 0
\end{equation}

\subsection{assuming $h$ is a linear system}

\begin{align}
    \begin{bmatrix} dh/dx1 & df/dx2 &\dots  \end{bmatrix} \mathbf{x} + c &= 0 \\
    \mathbf{b} \cdot \mathbf{x} + c &= 0 \label{eq:reduction_dof_linear1}
\end{align}

DOF therefore need to satisfy
\begin{align}
   \mathbf{b} \cdot (\mathbf{x}  + \Delta \mathbf{x}) + c &= 0 \label{eq:reduction_dof_linear2}
\end{align}   
leading to (eq  \ref{eq:reduction_dof_linear2}- eq \ref{eq:reduction_dof_linear1})
\begin{align}
   \mathbf{b} \cdot \Delta \mathbf{x} &= 0
\end{align} 
Therefore any vector orgonal to $\mathbf{b}$ is a degree of freedom.
   
\subsection{assuming $h$ is a quatdratic system}

\begin{align}
    \frac{1}{2} \mathbf{x}^T \mathbf{A} \mathbf{x} + \mathbf{b} \cdot \mathbf{x} + c &= 0 \label{eq:reduction_dof_quad1}
\end{align}
The changes in $\mathbf{x}$ are allowed which satisfy
\begin{align}
    \frac{1}{2} (\mathbf{x}+\Delta \mathbf{x})^T \mathbf{A} (\mathbf{x}+\Delta \mathbf{x}) + \mathbf{b} \cdot (\mathbf{x}+\Delta \mathbf{x}) + c &= 0 \label{eq:reduction_dof_quad2} \\
    \frac{1}{2} (\mathbf{x}+\Delta \mathbf{x})^T  ( \mathbf{A} \mathbf{x}+ \mathbf{A} \Delta \mathbf{x}) + \mathbf{b} \cdot (\mathbf{x}+\Delta \mathbf{x}) + c &= 0 \\
    \frac{1}{2} (\mathbf{x}^T \mathbf{A} \mathbf{x} + \mathbf{x}^T \mathbf{A} \Delta \mathbf{x} + \Delta\mathbf{x}^T\mathbf{A}\mathbf{x}+ \Delta\mathbf{x}^T\mathbf{A} \Delta \mathbf{x}) + \mathbf{b} \cdot (\mathbf{x}+\Delta \mathbf{x}) + c &= 0 
\end{align}
since $\frac{1}{2} \mathbf{x}^T \mathbf{A} \mathbf{x} + \mathbf{b} \cdot \mathbf{x} + c = 0$,
\begin{align}
    \frac{1}{2} (\mathbf{x}^T \mathbf{A} \Delta \mathbf{x} + \Delta\mathbf{x}^T\mathbf{A}\mathbf{x}+ \Delta\mathbf{x}^T\mathbf{A} \Delta \mathbf{x}) + \mathbf{b} \cdot \Delta \mathbf{x} &= 0 
\end{align}

furthmore assuming that $\mathbf{x}^T \mathbf{A} \Delta \mathbf{x} = \Delta\mathbf{x}^T\mathbf{A}\mathbf{x}$ ($\mathbf{A} = \mathbf{A} ^T)$ gives

\begin{align}
    \frac{1}{2} \Delta\mathbf{x}^T\mathbf{A} \Delta \mathbf{x} + (\mathbf{b}+\mathbf{x}^T \mathbf{A}) \cdot \Delta \mathbf{x} &= 0 
\end{align}


Find solutions for $\Delta \mathbf{x}$ allows for the degrees-of-freedom of the resulting system to be determined.
By definition these DOFs can be alterted without violating $h(\mathbf{x})$(eq. \ref{eq:reduction_dof_quad1}), hence not requiring the previous constraints to be resatisfied/resolved.

Scenarios do occur however, where $h(\mathbf{x}$ is a complicated functional which is not quatdratic.


\subsection{trail and error approach}

Here the approach is to pass dimensions of $\mathbf{x}$ directly through, to create non-perfect or false degrees of freedom.
The passed through dimensions of $\mathbf{y}$ when altered require that the previous constraints in the system be resolved.
The approach inside assembly 2 is determined the $\mathbf{y}$ with largest number of dimensions, which can passed through and changed, while still be able to satisfy previous constraints.

Order probably maters, but anyway, the going to be assumed that order does not matter.

\section{generating repairing/orthogonal basis functions}

Use the Gram–Schmidt process https://en.wikipedia.org/wiki/Gram\%E2\%80\%93Schmidt\_process :
the Gram–Schmidt process is a method for orthonormalising a set of vectors in an inner product space, most commonly the Euclidean space $\Re^n$.






\end{document}
